import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Room } from 'src/room/entities/room.entity';
import { User } from 'src/user/entities/user.entity';
import { In, Repository } from 'typeorm';
import { UpdateStartDto } from './dto/update-start.dto';
import { Start } from './entities/start.entity';

@Injectable()
export class StartService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    @InjectRepository(Start)
    private readonly satrtRepository: Repository<Start>,
    @InjectRepository(Room)
    private readonly roomRepository: Repository<Room>,
  ) {}
  //创建收藏
  async create(roomId: number, openId: string) {
    let user = await this.userRepository.find({ where: { openId } });
    let userId = user[0].id;
    let start = new Start();
    start.roomId = roomId;
    start.userId = userId;
    return await this.satrtRepository.save(start);
  }
  //查询用户所有收藏
  async findAll(openId: string) {
    let user = await this.userRepository.find({ where: { openId } });
    let userId = user[0].id;
    return await this.satrtRepository.find({ where: { userId } });
  }

  //查询用户收藏和房间信息
  async findStartRoom(openId: string) {
    let user = await this.userRepository.find({ where: { openId } });
    let userId = user[0].id;
    let roomList = await this.satrtRepository.find({ where: { userId } });
    let data = roomList.map((item) => {
      return item.roomId;
    });
    return await this.roomRepository.find({
      relations: ['photos'],
      where: { id: In(data) },
    });
  }
  //取消收藏
  async cancelStart(roomId: number, openId: string) {
    let user = await this.userRepository.find({ where: { openId } });
    let userId = user[0].id;
    return await this.satrtRepository.delete({ userId, roomId });
  }

  remove(id: number) {
    return `This action removes a #${id} start`;
  }
}
